package com.epalmpay.commom;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.epalmpay.exception.ApplicationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.ButtonOutDTO;
import com.epalmpay.dto.system.LoginInInDTO;
import com.epalmpay.dto.system.LoginInOutDTO;
import com.epalmpay.dto.system.MenuInDTO;
import com.epalmpay.dto.system.RoleOutDTO;
import com.epalmpay.dto.system.SMSInDTO;
import com.epalmpay.dto.system.UserInDTO;
import com.epalmpay.dto.system.UserOutDTO;
import com.epalmpay.entity.Button;
import com.epalmpay.entity.Menu;
import com.epalmpay.entity.Role;
import com.epalmpay.entity.Userbase;
import com.epalmpay.enumdef.CommonEnum;
import com.epalmpay.service.management.IButtonService;
import com.epalmpay.service.management.IMenuService;
import com.epalmpay.service.management.IRoleService;
import com.epalmpay.service.management.IUserService;
import com.epalmpay.service.sms.ISMSService;
import com.epalmpay.util.PropertyCopyUtil;

@Controller
@RequestMapping(value="/manager/user")
public class LoginController extends BaseController {
     
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource  
    private IRoleService roleService;  
	@Resource  
    private IUserService userService;  
	@Resource  
    private IMenuService menuService;  
	@Resource  
    private IButtonService buttonService;  
	@Resource  
    private ISMSService smsService;  
	
	
	/**
	 * 用户名密码验证
	 * 验证成功返回用户信息
	 */
	@RequestMapping(value="/loginIn")
	@ResponseBody
	public LoginInOutDTO login(LoginInInDTO loginIn){
		LoginInOutDTO loginInOut=new LoginInOutDTO();
		try {  
			Userbase user=new Userbase();
			user.setUsername(loginIn.getUsername()); 
			user.setPassword(loginIn.getPassword()); 
			List<Userbase> userlist=userService.getUserByNameAndPwd(user);
			//对象反射复制
			if(userlist.size()==1){
				Userbase users=userlist.get(0);
				if(users.getStatus()==1){
					PropertyCopyUtil.copyPropertiesExclude(users,loginInOut);
					loginInOut.setGroupName(users.getGroup().getMerchantName()); 
					loginInOut.setErrorCode(Rescode.SUCCESS, "登录成功");
				}else{
					loginInOut.setErrorCode(Rescode.LOCKING_ERROR, "账户被锁定,请与管理员联系");
				}
			}else{
				loginInOut.setErrorCode(Rescode.PASSWORD_ERROR, "密码错误");
			}
		} catch (Exception e) { 
			loginInOut.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return loginInOut;
	}
	
	
	/**
	 * 通过用户ID读取用户信息和角色信息
	 * 
	 */
	@RequestMapping(value="/userAndRoleByIdIn")
	@ResponseBody
	public UserOutDTO userAndRoleByIdIn(UserInDTO userIn){
		UserOutDTO userOutDTO=new UserOutDTO();
		try {  
			Userbase user=userService.getUserAndRoleById(userIn.getId());
			//采取对象反射复制
			if(user!=null){
				PropertyCopyUtil.copyPropertiesExclude(user,userOutDTO,"role","page");
				if(user.getRole()!=null){
					RoleOutDTO roleOutDTO=new RoleOutDTO();
					roleOutDTO.setRoleId(user.getRole().getRoleId());
					roleOutDTO.setRoleName(user.getRole().getRoleName());
					roleOutDTO.setRights(user.getRole().getRights());
					userOutDTO.setRole(roleOutDTO);
				}
				userOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			}
			
		} catch (Exception e) { 
			userOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return userOutDTO;
	}
	
	
		/**
		 * 获取所有菜单信息
		 * 
		 */
		@RequestMapping(value="/listAllMenuQx")
		@ResponseBody
		public List<Menu>  listAllMenuQx(MenuInDTO menuInDTO){
			try {  
				List<Menu> listMenu=menuService.listAllMenuQx(menuInDTO.getMenuId().toString(),menuInDTO.getMenuFlag());
				return listMenu;
			} catch (Exception e) { 
				e.printStackTrace();
			}
			return null;
		}
		
		
		
		/**
		 * 通过商户id  获取所有模板菜单信息 同上 做递归处理
		 * 
		 */
		@RequestMapping(value="/listAllModelMenu")
		@ResponseBody
		public List<Menu>  listAllModelMenu(MenuInDTO menuInDTO){
			try {  
				List<Menu> listMenu=menuService.listAllModelMenu(menuInDTO.getMenuId().toString(),menuInDTO.getMenuFlag(),
						menuInDTO.getGroupId(),menuInDTO.getMenuState(),null);
				return listMenu;
			} catch (Exception e) { 
				e.printStackTrace();
			}
			return null;
		}
		
		
		/**
		 * 
		 * 增删改查权限
		 * 
		 */
		@RequestMapping(value="/getUQX")
		@ResponseBody
		public ButtonOutDTO getUQX(UserOutDTO userIn){
			ButtonOutDTO buttonOutDTO=new ButtonOutDTO();
			try {  
				Role role=roleService.findObjectByUserName(userIn.getUsername());//获取角色信息
				RoleOutDTO roleOutDTO=new RoleOutDTO();
				if(role!=null){
					roleOutDTO.setAddQx(role.getAddQx());
					roleOutDTO.setChaQx(role.getChaQx());
					roleOutDTO.setDelQx(role.getDelQx());
					roleOutDTO.setEditQx(role.getEditQx());
				}
				buttonOutDTO.setRoleOutDTO(roleOutDTO);
				buttonOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功"); 
				return buttonOutDTO;
			} catch (Exception e) { 
				buttonOutDTO.setErrorCode(Rescode.FAIL, "查询异常"); 
				e.printStackTrace();
			}
			buttonOutDTO.setErrorCode(Rescode.FAIL, "查询异常"); 
			return buttonOutDTO;
		}
	
		/**
		 * 按钮权限
		 * @param userIn
		 * @return
		 */
		@RequestMapping(value="/getButton")
		@ResponseBody
		public ButtonOutDTO getButton(UserOutDTO userIn){
			ArrayList<ButtonOutDTO> buttonList=new ArrayList<ButtonOutDTO>();
			ButtonOutDTO buttonOutDTO=new ButtonOutDTO();
			try {  
				RoleOutDTO roleOutDTO=new RoleOutDTO();
				if(userIn.getUsername().equals("admin")||userIn.getUserType().equals(CommonEnum.UserType.BusinessManager.getType())
						||userIn.getUserType().equals(CommonEnum.UserType.Agents.getType())||userIn.getUserType().equals(CommonEnum.UserType.Store.getType())){
					List<Button> list=buttonService.AlllistButtonsName();//获取按钮权限
					for (int i = 0; i < list.size(); i++) {
						ButtonOutDTO button=new ButtonOutDTO();
						button.setQxName(list.get(i).getQxName()); 
						buttonList.add(button);
						buttonOutDTO.setButtonList(buttonList);
					}
					buttonOutDTO.setRoleOutDTO(roleOutDTO);
					buttonOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功"); 
				}else{
					List<Button> list=buttonService.listButtonsName(userIn.getUsername());
					for (int i = 0; i < list.size(); i++) {
						ButtonOutDTO button=new ButtonOutDTO();
						button.setQxName(list.get(i).getQxName()); 
						buttonList.add(button);
						buttonOutDTO.setButtonList(buttonList);
					}
					buttonOutDTO.setRoleOutDTO(roleOutDTO);
					buttonOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功"); 
				}
				return buttonOutDTO;
			} catch (Exception e) { 
				buttonOutDTO.setErrorCode(Rescode.FAIL, "查询异常"); 
				e.printStackTrace();
			}
			buttonOutDTO.setErrorCode(Rescode.FAIL, "查询异常"); 
			return buttonOutDTO;
		}
		
		
	
		/**
		 * 获取代理商或门店id
		 * 
		 */
		@RequestMapping(value="/shopOrAgent")
		@ResponseBody
		public UserOutDTO shopOrAgent(UserInDTO userIn){
			UserOutDTO  userOutDTO=null;
			try {
			    userOutDTO =userService.shopOrAgent(userIn);
				userOutDTO.setErrorCode(Rescode.SUCCESS, "查询成功"); 
				return userOutDTO;
			} catch (Exception e) {
				e.printStackTrace();
				userOutDTO.setErrorCode(Rescode.FAIL, "查询异常"); 
			}
			userOutDTO.setErrorCode(Rescode.FAIL, "查询异常"); 
			return userOutDTO;
		}


	/**
	 * 通过账户手机判断是否存在
	 *
	 */
	@RequestMapping(value="/queryLoginNameByPhoneAndName")
	@ResponseBody
	public UserOutDTO queryLoginNameByPhoneAndName(UserInDTO userInDTO){
		UserOutDTO userOutDTO=new UserOutDTO();
		try {
			userOutDTO= userService.queryLoginNameByPhoneAndName(userInDTO);
			userOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
		} catch (ApplicationException ae) {
			ae.printStackTrace();
			userOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
			logger.error(ae);
		}catch (Exception e) {
			e.printStackTrace();
			userOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return userOutDTO;
	}
		
	/**
	 * 忘记找回密码
	 *
	 */
	@RequestMapping(value="/findPassword")
	@ResponseBody
	public UserOutDTO findPassword(UserInDTO userInDTO){
		UserOutDTO userOutDTO=new UserOutDTO();
		try {
			userOutDTO= userService.queryDataByNameAndPhone(userInDTO);
			userOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

		} catch (ApplicationException ae) {
			ae.printStackTrace();
			logger.error(ae);
			userOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		}catch (Exception e) {
			userOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return userOutDTO;
	}

  //
	
	
	
	/**
	 * sendSMSMessage
	 *
	 */
	@RequestMapping(value="/sendSMSMessage")
	@ResponseBody
	public BaseOutDTO sendSMSMessage(@RequestBody SMSInDTO smsInDTO){
		BaseOutDTO baseOutDTO=new BaseOutDTO();
		try {
			Long groupId=smsInDTO.getGroupId();
			String code=smsInDTO.getCode(); 
			String templateType=smsInDTO.getTemplateType();  
			String strMobile=smsInDTO.getStrMobile();
			Map<String,String> param=smsInDTO.getParams();
			boolean  result= smsService.sendRegionCode(groupId, code, templateType, strMobile, param);

			if(result){
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			}else{
				baseOutDTO.setErrorCode(Rescode.FAIL, "发送失败");
			}
			

		} catch (ApplicationException ae) {
			logger.error(ae);
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, ae.getMessage());
		}catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.FAIL, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

}
